package com.cata.mall.common.config;

import com.cata.mall.common.service.RedisService;
import com.cata.mall.common.service.impl.RedisServiceImpl;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * @作者: Tao
 * @日期: 2025/10/10
 * @时间: 15:09
 * @描述: Redis基础配置<pre>
 */
public class BaseRedisConfig {

    /**
     * 创建并配置RedisTemplate实例
     *
     * @param redisConnectionFactory Redis连接工厂，用于创建Redis连接
     * @param redisSerializer Redis序列化器，用于序列化和反序列化对象
     * @return 配置完成的RedisTemplate实例
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer<Object> redisSerializer) {
        // 创建RedisTemplate实例并设置连接工厂
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 配置键和值的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(redisSerializer);

        // 初始化RedisTemplate属性
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * 创建Redis序列化器Bean
     * -
     * 该方法配置并返回一个用于Redis数据序列化和反序列化的Jackson2JsonRedisSerializer实例。
     * 通过自定义ObjectMapper配置，支持将Java对象序列化为JSON格式存储到Redis中，
     * 并能够在读取时正确反序列化回Java对象。
     *
     * @return RedisSerializer<Object> 配置好的JSON序列化器实例
     */
    @Bean
    public RedisSerializer<Object> redisSerializer() {
        // 创建Jackson2JsonRedisSerializer实例，用于处理Object类型的序列化
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);

        // 配置ObjectMapper以支持完整的JSON序列化功能
        ObjectMapper objectMapper = new ObjectMapper();
        // 设置所有属性都可见，确保私有字段也能被序列化
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 启用默认类型推断，支持非final类的多态序列化
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);

        // 将配置好的ObjectMapper设置到序列化器中
        serializer.setObjectMapper(objectMapper);
        return serializer;
    }

    /**
     * 创建并配置Redis缓存管理器
     *
     * @param redisConnectionFactory Redis连接工厂，用于建立与Redis服务器的连接
     * @return 配置好的Redis缓存管理器实例
     */
    @Bean
    public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
        // 创建非阻塞的Redis缓存写入器
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置Redis缓存有效期为1天
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer())).entryTtl(Duration.ofDays(1));
        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }

    /**
     * 创建并配置Redis服务
     *
     * @return 配置好的Redis服务实例
     */
    @Bean
    public RedisService redisService(){
        return new RedisServiceImpl();
    }
}
